cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(recipe18 LANGUAGES NONE)

####################################################
# find_file
####################################################

# 尝试在以下位置查找 config.ini：
# 1. 项目源码目录 (CMAKE_SOURCE_DIR)
# 2. 项目构建目录 (CMAKE_BINARY_DIR)
# 3. 通过环境变量 MY_APP_CONFIG_DIR 指定的目录
# 4. /etc/my_app/ (Linux/macOS 常见配置路径)
# 5. /usr/local/etc/my_app/ (另一个常见配置路径)
find_file(MY_APP_CONFIG_FILE
          NAMES config.ini
          HINTS "$ENV{MY_APP_CONFIG_DIR}"
          PATHS "${CMAKE_SOURCE_DIR}"
                "${CMAKE_BINARY_DIR}"
                "/etc/my_app"
                "/usr/local/etc/my_app"
          REQUIRED  # 如果找不到，则报错停止配置
          DOC "Path to the application configuration file (config.ini)"
)

# 检查是否成功找到文件
if(MY_APP_CONFIG_FILE)
    message(STATUS "Found config.ini at: ${MY_APP_CONFIG_FILE}")
    # 可以在这里将路径添加到编译定义或安装规则中
    # add_definitions(-DMY_CONFIG_PATH="${MY_APP_CONFIG_FILE}")
else()
    message(FATAL_ERROR "config.ini not found! Please set MY_APP_CONFIG_DIR environment variable or place it in a standard location.")
endif()

#################################################################
# find_library
#################################################################

# 查找 OpenSSL 的 SSL 组件
find_library(OPENSSL_SSL_LIBRARY
             NAMES ssl ssleay32 # 尝试查找 ssl 或 ssleay32
             DOC "Path to OpenSSL SSL library"
)

# 查找 OpenSSL 的 Crypto 组件
find_library(OPENSSL_CRYPTO_LIBRARY
             NAMES crypto libeay32 # 尝试查找 crypto 或 libeay32
             DOC "Path to OpenSSL Crypto library"
)

# 检查两个组件是否都找到
if(OPENSSL_SSL_LIBRARY_FOUND AND OPENSSL_CRYPTO_LIBRARY_FOUND)
    message(STATUS "Found OpenSSL SSL library: ${OPENSSL_SSL_LIBRARY}")
    message(STATUS "Found OpenSSL Crypto library: ${OPENSSL_CRYPTO_LIBRARY}")
    set(OPENSSL_FOUND TRUE) # 设置一个总体的 FOUND 变量
    set(OPENSSL_LIBRARIES "${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}") # 组合库路径
else()
    message(WARNING "OpenSSL libraries not found. Some features might be disabled.")
    set(OPENSSL_FOUND FALSE)
endif()

# 根据是否找到 OpenSSL 来决定构建行为
if(OPENSSL_FOUND)
    # 假设有一个名为 'my_secure_app' 的可执行目标
    # 链接 OpenSSL 库
    # target_link_libraries(FOUND)
    message(STATUS "Found OpenSSL SSL library: ${OPENSSL_SSL_LIBRARY}")
    message(STATUS "Found OpenSSL Crypto library: ${OPENSSL_CRYPTO_LIBRARY}")
    set(OPENSSL_FOUND TRUE) # 设置一个总体的 FOUND 变量
    set(OPENSSL_LIBRARIES "${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}") # 组合库路径
else()
    message(WARNING "OpenSSL libraries not found. Some features might be disabled.")
    set(OPENSSL_FOUND FALSE)
endif()

# 根据是否找到 OpenSSL 来决定构建行为
if(OPENSSL_FOUND)
    # 链接 OpenSSL 库
    # target_link_libraries(my_secure_app PRIVATE ${OPENSSL_LIBRARIES})
endif()

######################################################################
# find_path
######################################################################
# 使用 find_path 查找 string 头文件的路径
find_path(IOS_BASE_DIR
    NAMES string.h
    PATHS /usr/include /usr/local/include
    NO_DEFAULT_PATH  # 可选项，如果只想在提供的路径中查找
)

if(IOS_BASE_DIR)
    message(STATUS "Found string.h at: ${IOS_BASE_DIR}")
else()
    message(FATAL_ERROR "string.h not found")
endif()

######################################################
# find_program
######################################################
# 查找 Git 可执行程序
# GIT_EXECUTABLE 将存储找到的 Git 程序的完整路径
find_program(GIT_EXECUTABLE
             NAMES git # 查找名为 git 的程序
             DOC "Path to the Git executable"
)

# 检查是否成功找到 Git
if(GIT_EXECUTABLE)
    message(STATUS "Found Git executable: ${GIT_EXECUTABLE}")
    # 可以在这里使用 execute_process 来获取 Git 版本或执行其他 Git 命令
    # execute_process(COMMAND ${GIT_EXECUTABLE} --version OUTPUT_VARIABLE GIT_VERSION)
    # message(STATUS "Git Version: ${GIT_VERSION}")
else()
    message(WARNING "Git executable not found. Some version control features might be unavailable.")
endif()
